package com.project.finance.role.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.finance.common.mapper.TbSysPermissionMapper;
import com.project.finance.common.po.TbSysPermission;
import com.project.finance.common.vo.ResultMsg;
import com.project.finance.role.service.PermissionService;
import org.apache.shiro.authz.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class PermissionImpl implements PermissionService {
    @Autowired
    private TbSysPermissionMapper tbSysPermissionMapper;
    //查询
    @Override
    public List<TbSysPermission> selectAll() {
        return tbSysPermissionMapper.selectAll();
    }
    //添加
    @Override
    public int insert(TbSysPermission tbSysPermission) {
        return tbSysPermissionMapper.insert(tbSysPermission);
    }
    //删除
    @Override
    public int delete(int permissionId) {
        return tbSysPermissionMapper.deleteByPrimaryKey(permissionId);
    }
    //修改
    @Override
    public int update(TbSysPermission tbSysPermission) {
        return tbSysPermissionMapper.updateByPrimaryKey(tbSysPermission);
    }

    @Override
    public ResultMsg listPermissionByPage(int pageSize, int pageNum) {
        ResultMsg resultMsg = new ResultMsg();

        PageHelper.startPage(pageNum,pageSize);

        List<TbSysPermission> tbSysPermissions = tbSysPermissionMapper.selectAll();

        //获取结果集的分页信息
        PageInfo pageInfo = new PageInfo(tbSysPermissions);
        //获取总记录数
        long total = pageInfo.getTotal();
        resultMsg.setTatol(total);
        resultMsg.setData(tbSysPermissions);
        return resultMsg;
    }

    //根据账号来查询用户信息
    @Override
    public List<TbSysPermission> getSysPermission(String pername) {
        Example example = new Example(TbSysPermission.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andLike("perName","%"+pername+"%");
        List<TbSysPermission> tbSysPermissions=tbSysPermissionMapper.selectByExample(example);
        return tbSysPermissions;
    }

    @Override
    public List<TbSysPermission> findPermByLoginName(String loginName) {
        return tbSysPermissionMapper.selectPermission(loginName);
    }
}
